<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form action="">
        <p>
            <label for="">用户：</label>
            <input type="text" name="username" id="user">
        </p>
        <p>
            <label for="">密码：</label>
            <input type="text" name="pass" id="pwrod">
        </p>
        <button>登录</button>
    </form>
    <script>
        var form = document.querySelector('form');
        var user = document.querySelector('#user');
        var pwrod = document.querySelector('#pwrod');
        form.onsubmit = function () {
            let e = window.event;
            // 阻止form表单提交就会刷新浏览器的默认行为
            e.preventDefault();
            let ajax = new XMLHttpRequest();
            // 当请求为POST请求时，参数写在send方法里面
            ajax.open('post', '../2.2.1denglu.php');
            // POST请求的时候，需要配合 请求头 的设置，不然参数不能进入到请求体中
            ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')

            ajax.send(`username=${user.value}&pass=${pwrod.value}`);

            ajax.onload = function () {
                console.log(ajax.responseText);
            }
        }
    </script>
</body>

</html>